<template>
  <div>
    <vxe-modal v-model="isVisiable" v-if="isVisiable" width="55vw" show-footer destroy-on-close :mask="false">
      <template #title>
        <span class="text-bold">结果录入</span>
      </template>
      <template #default>
        <div style="height: 70vh" :class='resultIsDisabled() ? "no-edit" : ""'>
          <vxe-form :data="bacTestResultForm" title-width="100px">
            <vxe-form-gather span="8">
              <vxe-form-item title="样本号" field="sampleNo" span="24">
                <template #default="params">
                  <vxe-input v-model="params.data.sampleNo" type="text" disabled placeholder="样本号" clearable></vxe-input>
                </template>
              </vxe-form-item>
            </vxe-form-gather>
            <vxe-form-gather span="8">
              <!-- <vxe-form-item title="菌株号" field="isolateNum" span="24" :item-render="{}" title-overflow>
               <template #default="params">
                <vxe-select v-model="params.data.strainNo" placeholder="请选择菌株号" @change="stainChange" clearable>
                  <vxe-option v-for="(item, index) in bacCultiObserList" :key="index" :label="item.strainName"
                    :value="item.strainCode" ></vxe-option>
                </vxe-select>
                </template>
              </vxe-form-item> -->
              <vxe-form-item title="菌株号" field="strainNo" span="24" :item-render="{}" title-overflow>
                <template #default="params">
                  <vxe-select v-model="params.data.strainNo" placeholder="请选择菌株号" @change="stainChange" clearable>
                    <vxe-option v-for="(item, index) in bacCultiObserList" :key="index" :label="item.strainName" :value="item.strainCode"></vxe-option>
                  </vxe-select>
                </template>
              </vxe-form-item>

            </vxe-form-gather>
            <vxe-form-gather span="8">
              <vxe-form-item title="菌落计数" field="urinColoCount" span="24" :item-render="{}" title-overflow>
                <template #default="params">
                  <!-- <vxe-input v-model="params.data.urinColoCount" type="text" placeholder="菌落计数" clearable></vxe-input> -->
                  <VxePulldownSelectionWithNa style="width: 100%" :isChild="false" :url="'/api/bac/bac-union-colo-count/findBacUnionColoCountList'" @confirmClick="SelectRowClick" :tableColumn="[
                      { type: 'seq', title: '#', width: '30px' },
                      { field: 'name', title: '名称' },
                      { field: 'code', title: '编码', width: '60' }
                    ]" :aimKeys="[{ aimKey: 'urinColoCount', rowKey: 'name' }]" :displayTexts="['urinColoCount']" :displayTextObj="params.data.urinColoCount ? params.data.urinColoCount : ''" />
                </template>
              </vxe-form-item>
            </vxe-form-gather>
            <vxe-form-gather span="8">
              <vxe-form-item title="鉴定结果" span="24">
                <template #title>
                  <span style="color: red">鉴定结果</span>
                </template>
                <template #default="params">
                  <VxePulldownSelectionByDictType v-if="isDescOptionResultFlag" :isChild="false" :isPage="true" :displayTexts="['germNa']" :displayTextObj="params.data.germNa ? params.data.germNa : ''" :tableColumn="[{ title: '#', type: 'seq', width: '30' }, { title: '微生物代号', field: 'germNo' }, { title: '名称', field: 'germNa' }]" :aimKeys="[{ aimKey: 'germNo', rowKey: 'germNo' }, { aimKey: 'germNa', rowKey: 'germNa' }]" :url="'/bac/germ/dict/getGermsByParams'" :dictTypeCode="'germType'" @confirmClick="SelectRowClick" />
                  <VxePulldownSelectionWithNa v-else style="width: 100%" :isChild="false" :url="'/api/bac/bac-desc-option/findBacDescOptionList'" @confirmClick="SelectRowClick" @manualInput="manualInput" :tableColumn="[
                      { type: 'seq', title: '#', width: '30px' },
                      { field: 'optNa', title: '名称' },
                      { field: 'optNo', title: '编码' }
                    ]" :aimKeys="[
  { aimKey: 'germNa', rowKey: 'optNa' },
]" :displayTexts="['germNa']" :displayTextObj="params.data.germNa ? params.data.germNa : ''" />
                </template>
              </vxe-form-item>
            </vxe-form-gather>
            <vxe-form-gather span="8">
              <vxe-form-item title="菌群比例" field="bacRate" span="24" :item-render="{}" title-overflow="title">
                <template #default="params">
                  <vxe-input v-model="params.data.bacRate" type="float" placeholder="菌群比例" clearable></vxe-input>
                </template>
              </vxe-form-item>
            </vxe-form-gather>
            <vxe-form-gather span="8">
              <vxe-form-item title="耐药表型" field="resiPhenNa" span="24" :item-render="{}" title-overflow="ellipsis">
                <template #default="params">
                  <VxePulldownSelectionWithNa style="width: 100%" :isChild="false" :url="'/lis/bac/base/resistant-phenotype/dict/findDictList'" @confirmClick="SelectRowClick" :tableColumn="[
                      { type: 'seq', title: '#', width: '30px' },
                      { field: 'name', title: '名称' },
                      { field: 'code', title: '编码' },
                      { field: 'resistantTypeNa', title: '耐药类型' }
                    ]" :aimKeys="[
  { aimKey: 'resiPhenId', rowKey: 'id' },
  { aimKey: 'resiPhenNa', rowKey: 'name' },
  { aimKey: 'resiPhenNo', rowKey: 'code' },
  { aimKey: 'resistantTypeNo', rowKey: 'resistantTypeNo' }
]" :displayTexts="['resiPhenNa']" :displayTextObj="params.data.resiPhenNa ? params.data.resiPhenNa : ''" />
                </template>
              </vxe-form-item>
            </vxe-form-gather>
            <vxe-form-gather span="8">
              <vxe-form-item title="感染部位" field="infeSiteNa" span="24" :item-render="{}" title-overflow="ellipsis">
                <template #default="params">
                  <VxePulldownSelectionWithNa ref="testResultSelectRef" style="width: 100%" :isChild="false" :url="'/lis/bac/base/site-of-infection/dict/findDictList'" @confirmClick="SelectRowClick" :tableColumn="[
                      { type: 'seq', title: '#', width: '30px' },
                      { field: 'name', title: '名称' },
                      { field: 'code', title: '编码', width: '60px' }
                    ]" :aimKeys="[
  { aimKey: 'infeSiteId', rowKey: 'id' },
  { aimKey: 'infeSiteNa', rowKey: 'name' },
  { aimKey: 'infeSiteNo', rowKey: 'code' }
]" :displayTexts="['infeSiteNa']" :displayTextObj="params.data.infeSiteNa ? params.data.infeSiteNa : ''" />

                </template>
              </vxe-form-item>
            </vxe-form-gather>
            <vxe-form-gather span="8">
              <vxe-form-item title="操作时间" field="operateDate" span="24" :item-render="{}" title-overflow="ellipsis">
                <template #default="params">
                  <vxe-input v-model="params.data.operateDate" type="datetime" placeholder="操作时间" clearable></vxe-input>
                </template>
              </vxe-form-item>
            </vxe-form-gather>
            <vxe-form-gather span="8">
              <vxe-form-item title="操作人" field="operateUserNo" span="24" :item-render="{}" title-overflow="ellipsis">
                <template #default="params">
                  <vxe-input v-model="params.data.operateUserNo" type="text" disabled placeholder="操作人" clearable></vxe-input>
                </template>
              </vxe-form-item>
            </vxe-form-gather>
            <vxe-form-gather span="8">
              <vxe-form-item title="结果序号" field="isolateNum" span="24" :item-render="{}" title-overflow>
                <template #default="params">
                  <vxe-select v-model="params.data.isolateNum" placeholder="请选择结果序号" disabled clearable>
                    <vxe-option value="1" label="1"></vxe-option>
                    <vxe-option value="2" label="2"></vxe-option>
                    <vxe-option value="3" label="3"></vxe-option>
                    <vxe-option value="4" label="4"></vxe-option>
                    <vxe-option value="5" label="5"></vxe-option>
                    <vxe-option value="6" label="6"></vxe-option>
                    <vxe-option value="7" label="7"></vxe-option>
                    <vxe-option value="8" label="8"></vxe-option>
                    <vxe-option value="9" label="9"></vxe-option>
                    <vxe-option value="10" label="10"></vxe-option>
                    <vxe-option value="11" label="11"></vxe-option>
                    <vxe-option value="12" label="12"></vxe-option>
                    <vxe-option value="13" label="13"></vxe-option>
                    <vxe-option value="14" label="14"></vxe-option>
                    <vxe-option value="15" label="15"></vxe-option>
                    <vxe-option value="16" label="16"></vxe-option>
                    <vxe-option value="17" label="17"></vxe-option>
                    <vxe-option value="18" label="18"></vxe-option>
                    <vxe-option value="19" label="19"></vxe-option>
                    <vxe-option value="20" label="20"></vxe-option>
                  </vxe-select>
                </template>
              </vxe-form-item>
            </vxe-form-gather>
            <vxe-form-gather span="24">
              <vxe-form-item title="是否传染病" field="isContagion" span="24" :item-render="{}" title-overflow="title">
                <template #default="params">
                  <el-switch v-model="params.data.isContagion" inline-prompt active-text="是" inactive-text="否"></el-switch>
                  <!-- <vxe-switch v-model="params.data.isAlarm" inline-prompt open-label="是" close-label="否" open-value="1" close-value="0"></vxe-switch> -->
                </template>
              </vxe-form-item>
            </vxe-form-gather>

            <vxe-form-gather span="8">
              <vxe-form-item title="细菌危急值" field="isAlarm" span="24" :item-render="{}" title-overflow="title">
                <template #default="params">
                  <el-switch v-model="params.data.isAlarm" inline-prompt active-text="是" inactive-text="否"></el-switch>
                  <!-- <vxe-switch v-model="params.data.isAlarm" inline-prompt open-label="是" close-label="否" open-value="1" close-value="0"></vxe-switch> -->
                </template>
              </vxe-form-item>
            </vxe-form-gather>

            <vxe-form-gather span="24">
              <vxe-form-item title="危急值描述" field="alarmText" span="24" :item-render="{}" title-overflow="ellipsis">
                <template #default="params">
                  <!-- <vxe-textarea v-model="params.data.alarmText" placeholder="危急值描述" :autosize="{ minRows: 4, maxRows: 16 }" resize="vertical"  @focus="showTypeCodeDrawer(params.data,'alarmText')" class='alarmText'></vxe-textarea> -->
                  <VxeRemarkDictSelection :isChild="false" :tableColumn="[
                    { field: 'code', title: '编码', width: `60px` },
                    { field: 'name', title: '名称', minWidth: `80px` }
                  ]" :url="`/api/sys/basDictDetail/findDictDetailList`" :displayTextObj="params.data.alarmText" @confirmClick="germAlarmConfirm" :dataObj="{ code: 'germAlarm' }"></VxeRemarkDictSelection>
                </template>
              </vxe-form-item>
            </vxe-form-gather>

            <vxe-form-gather span="8">
              <vxe-form-item title="药敏危急值" field="isDrugAlarm" span="24" :item-render="{}" title-overflow="title">
                <template #default="params">
                  <el-switch v-model="params.data.isDrugAlarm" inline-prompt active-text="是" inactive-text="否"></el-switch>
                  <!-- <vxe-switch v-model="params.data.isAlarm" inline-prompt open-label="是" close-label="否" open-value="1" close-value="0"></vxe-switch> -->
                </template>
              </vxe-form-item>
            </vxe-form-gather>

            <vxe-form-gather span="24">
              <vxe-form-item title="药敏危急值描述" field="drugAlarmText" span="24" :item-render="{}" title-overflow="ellipsis">
                <template #default="params">
                  <!-- <vxe-textarea v-model="params.data.drugAlarmText" placeholder="药敏危急值描述" :autosize="{ minRows: 4, maxRows: 16 }" resize="vertical"  @focus="showTypeCodeDrawer(params.data,'drugAlarmText')" class='drugAlarmText'></vxe-textarea> -->
                  <VxeRemarkDictSelection :isChild="false" :tableColumn="[
                    { field: 'code', title: '编码', width: `60px` },
                    { field: 'name', title: '名称', minWidth: `80px` }
                  ]" :url="`/api/sys/basDictDetail/findDictDetailList`" :displayTextObj="params.data.drugAlarmText" @confirmClick="drugAlarmConfirm" :dataObj="{ code: 'drugAlarm' }"></VxeRemarkDictSelection>
                </template>
              </vxe-form-item>
            </vxe-form-gather>

            <vxe-form-gather span="24">
              <vxe-form-item title="结果描述/备注" field="testResult" span="24" :item-render="{}" title-overflow="ellipsis">
                <template #default="params">
                  <vxe-textarea v-model="params.data.testResult" placeholder="结果描述" :autosize="{ minRows: 8, maxRows: 16 }" resize="vertical" @focus="showTypeCodeDrawer(params.data, 'xjpymsjg')" class='xjpymsjg'></vxe-textarea>
                  <!-- <vxe-input v-model="params.data.testResult" type="text" placeholder="结果描述" clearable></vxe-input> -->
                </template>
              </vxe-form-item>
            </vxe-form-gather>
            <vxe-form-gather span="24">
              <vxe-form-item title="专家评语" field="descText" span="24" :item-render="{}" title-overflow>
                <template #default="params">
                  <vxe-textarea v-model="params.data.descText" placeholder="专家评语" :autosize="{ minRows: 8, maxRows: 16 }" resize="vertical" @focus="showTypeCodeDrawer(params.data, 'xjpyzjpy')" class='xjpyzjpy'></vxe-textarea>
                  <!-- <vxe-input v-model="params.data.resultComment" type="text" disabled placeholder="操作人" clearable></vxe-input> -->
                </template>
              </vxe-form-item>
            </vxe-form-gather>
          </vxe-form>
        </div>
      </template>
      <template #footer>
        <vxe-toolbar>
          <template #buttons>

          </template>
          <template #tools>
            <vxe-button type="text" class="button-white" @click="cancelEvent">取消</vxe-button>
            <vxe-button type="text" class="button-blue" @click="confirmEvent" :disabled='resultIsDisabled()'>确定</vxe-button>
          </template>
        </vxe-toolbar>
      </template>
    </vxe-modal>
  </div>
  <BacFormEditDrawer ref="diagnosTempDrawerRef" @sltDiagnosTempInfo="selectDiagnosTempInfo">
  </BacFormEditDrawer>
</template>

<script setup lang="ts">
import { ref, reactive, computed, getCurrentInstance, defineProps } from 'vue';
import { formatDateTime, IsEmpty } from '@/utils/common';
import { Splitpanes, Pane } from 'splitpanes';
import VxePulldownSelectionWithNa from '@/components/VxePulldownSelectionWithNa.vue';
import VxePulldownSelectionByDictType from '@/components/VxePulldownSelectionByDictType.vue';
import BacFormEditDrawer from './BacFormEditDrawer.vue';
import VxeRemarkDictSelection from '@/components/VxeRemarkDictSelection.vue';
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';

let isDescOptionResultFlag = ref(false); /**是否描述性结果，默认添加阴性结果 */
let { proxy }: any = getCurrentInstance();
let diagnosTempDrawerRef = ref(null);
let isVisiable = ref(false);
let wkflwList: any[] = reactive([
  { code: '001', wk: '一般细菌及药敏鉴定方案流程' },
  { code: '002', wk: '支原体及药敏鉴定方案流程' },
  { code: '003', wk: '病毒及药敏鉴定' },
  { code: '004', wk: '寄生虫及药敏鉴定' }
]); /**流程方案集合 */
let wkflwNodeList: any[] = reactive([
  { code: '001', node: '培养' },
  { code: '002', node: '染色' },
  { code: '003', node: '观察形态' },
  { code: '004', node: '药敏试验' },
  { code: '005', node: '结论报告' }
]); /**流程方案节点集合 */

let bacCultiObserList: any[] = reactive([]); /**细菌菌株 */

const testResultSelectRef = ref(null); //用户选择组件el对象

const props = defineProps({});
const wkflwRef = ref(null); /**流程方案列表对象 */
//const wkflwNodeRef = ref(null);/**流程方案节点列表对象 */

let labMainInfoObj: any = reactive({});
let bacTestResultForm: any = reactive({}); /**细菌鉴定结果表单对象 */

defineExpose({
  showModal /**打开选择方案的弹窗 */,
  editShowModal /**页面编辑/切换显示隐藏 */
});
/**流程方案单元格点击事件 */
function wkflwClick({ row, rowIndex }) {
  wkflwRef.value?.setRadioRow(row);
}
/**流程方案行单选按钮选中事件 */
function setSelectRow(row) {
  wkflwRef.value?.setRadioRow(row);
}
/**页面初始化/切换显示隐藏 */
function showModal(labmaininfo, isYangXResult = false, isolateNum = '1') {
  isDescOptionResultFlag.value = isYangXResult;
  isVisiable.value = true;
  Object.assign(labMainInfoObj, labmaininfo);
  const userInfo = localStorage.getItem('userInfo');
  const userInfoObj = JSON.parse(userInfo);
  Object.assign(bacTestResultForm, {
    id: null,
    wkFlwId: '',
    isolateNum: isolateNum,
    germNo: null,
    germNa: null,
    testResult: null,
    resiPhenNa: '',
    urinColoCount: null,
    bacRate: null,
    infeSiteNa: '',
    operateDate: formatDateTime(new Date(), 'yyyy-MM-dd hh:mm:ss'),
    operateUserNo: userInfoObj ? userInfoObj.realname : '',
    sampleda: formatDateTime(labMainInfoObj.sampleda, 'yyyy-MM-dd hh:mm:ss'),
    rptGroup: labMainInfoObj.rptGroup,
    sampleNo: labMainInfoObj.sampleNo,
    maininfoId: labMainInfoObj.id,
    infeSiteNo: null,
    infeSiteId: null,
    isAlarm: '0',
    alarmText: null,
    isDrugAlarm: '0',
    drugAlarmText: null,
    descText: null,
    strainNo: null
  });
  findRptCultiObserResult();
}
/**页面编辑/切换显示隐藏 */
function editShowModal(item, labmaininfo, isYangXResult = false) {
  /* 先清空，再赋值 */
  const userInfo = localStorage.getItem('userInfo');
  const userInfoObj = JSON.parse(userInfo);
  bacTestResultForm.id = null;
  bacTestResultForm.wkFlwId = '';
  bacTestResultForm.isolateNum = '';
  bacTestResultForm.germNo = null;
  bacTestResultForm.germNa = null;
  bacTestResultForm.testResult = null;
  bacTestResultForm.resiPhenNa = '';
  bacTestResultForm.urinColoCount = null;
  bacTestResultForm.bacRate = null;
  bacTestResultForm.infeSiteNa = '';
  bacTestResultForm.operateDate = formatDateTime(new Date(), 'yyyy-MM-dd hh:mm:ss');
  bacTestResultForm.operateUserNo = userInfoObj ? userInfoObj.realname : '';
  bacTestResultForm.sampleda = formatDateTime(labMainInfoObj.sampleda, 'yyyy-MM-dd hh:mm:ss');
  bacTestResultForm.rptGroup = labMainInfoObj.rptGroup;
  bacTestResultForm.sampleNo = labMainInfoObj.sampleNo;
  bacTestResultForm.maininfoId = labMainInfoObj.id;
  bacTestResultForm.infeSiteNo = null;
  bacTestResultForm.infeSiteId = null;
  bacTestResultForm.isAlarm = '0';
  bacTestResultForm.alarmText = null;
  bacTestResultForm.isDrugAlarm = '0';
  bacTestResultForm.drugAlarmText = null;
  bacTestResultForm.descText = null;
  bacTestResultForm.strainNo = null;
  isDescOptionResultFlag.value = isYangXResult;
  Object.assign(labMainInfoObj, labmaininfo);
  Object.assign(bacTestResultForm, item);
  isVisiable.value = true;
  proxy.$forceUpdate();
  findRptCultiObserResult();
}

/**查询菌株 */
function findRptCultiObserResult() {
  bacCultiObserList.length = 0;
  if (IsEmpty(labMainInfoObj.id)) {
    return;
  }
  const url = '/bac/strain/findStrain';
  let data = { mainInfoId: labMainInfoObj.id, rptGroup: labMainInfoObj.rptGroup, sampleda: formatDateTime(labMainInfoObj.sampleda, 'yyyy-MM-dd 00:00:00'), sampleNo: labMainInfoObj.sampleNo } as any;
  proxy.$http
    .get(url, { params: data })
    .then((res: any) => {
      if (res && res.data && res.data.length >= 0) {
        Object.assign(bacCultiObserList, res.data);
      }
    })
    .catch((err: any) => {});
}

function stainChange(e) {
  console.log('=========>', e.value);
  //根据菌株编号查询结果
  bacCultiObserList.find((item) => {
    if (item.strainCode == e.value) {
      bacTestResultForm.urinColoCount = item.strainCount;
    }
  });
}

/**判断使用哪个结果选择组件 */
function validateWitchResultComp(row) {
  let res = false;
  if ((IsEmpty(row.id) && IsEmpty(row.germNo)) || (!IsEmpty(row.id) && !isDescOptionResultFlag.value)) {
    res = true;
  }
  return res;
}
/**确认选择 */
function confirmEvent() {
  if (isDescOptionResultFlag.value && IsEmpty(bacTestResultForm.germNo)) {
    VxeMessage('请选择细菌鉴定结果', VxeNotifyType.error);
    return;
  }

  proxy.$emit('confirmSave', bacTestResultForm);

  isVisiable.value = false;
}

/**关闭窗体 */
function cancelEvent() {
  isVisiable.value = false;
}
/**鉴定结果选中事件 */
function SelectRowClick(e) {
  if (e.isChild) {
    e.aimKeys.forEach((m) => {
      bacTestResultForm[e.childIndex][m.aimKey] = e.row[m.rowKey];
    });
  } else {
    e.aimKeys.forEach((m) => {
      bacTestResultForm[m.aimKey] = e.row[m.rowKey];
      console.log(m.aimKey);
      if('resistantTypeNo'===m.aimKey){
        if(e.row.resistantTypeNo==='MDR'){
          bacTestResultForm.isAlarm = true;
        }else{
          bacTestResultForm.isAlarm = false;
        }
        
      }
      
    });
  }
}
//手动输入
function manualInput(m) {
  bacTestResultForm.germNa = m.data;
}
/**调起模板漂浮div选择框 */
function showTypeCodeDrawer(item, descType) {
  diagnosTempDrawerRef.value.initPage(item, descType);
}
/**判断是否审核，若审核范围ture */
function resultIsDisabled() {
  if (!IsEmpty(labMainInfoObj) && labMainInfoObj.isChk == '2') {
    return true;
  } else {
    return false;
  }
}

function germAlarmConfirm(value) {
  bacTestResultForm.alarmText = value;
}

function drugAlarmConfirm(value) {
  bacTestResultForm.drugAlarmText = value;
}
/*  */
function selectDiagnosTempInfo() {}
</script>

<style scoped lang="scss">
.custom-radio {
  font-size: 18px;
  cursor: pointer;
  user-select: none;
}

:deep(.vxe-form--item-title .vxe-form--item-title-label) {
  padding-left: 5px;
}

.no-edit {
  user-select: none;
  /* 禁止选择文本 */
  pointer-events: none;
  /* 禁止所有鼠标事件 */
}
</style>
